package GUI;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;

import GUI_CONTROLER.*;
import start.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.JTextPane;

public class CopyOfLoginUser extends JFrame {
	private JTextField textField;
	private JTextField textField_1;
	String user,pass;
	public int flag=-1;
	public DBhandler log;
	Gui_Student_Entry_Window StuMain;
	
	
	public CopyOfLoginUser(ClientConsole chat) {
		setAlwaysOnTop(false);
		setTitle("Login Window");
		setSize(1000,1000);
		setBounds(0, 0, 1000, 1000);
		
		final LoginUserC Controler = new LoginUserC(chat);
		getContentPane().setFont(new Font("Trebuchet MS", Font.PLAIN, 18));
		getContentPane().setLayout(null);
		//getContentPane().setSize(155, 20);
		
		JLabel lblLoginSystem = new JLabel("LOGIN SYSTEM");
		lblLoginSystem.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblLoginSystem.setForeground(Color.BLUE);
		lblLoginSystem.setBounds(130, 0, 141, 14);
		getContentPane().add(lblLoginSystem);
		
		textField = new JTextField();
		textField.setBounds(126, 37, 86, 20);
		getContentPane().add(textField);
		textField.setColumns(10);
		
		this.setVisible(true);
		JLabel lblUserid = new JLabel("USER_ID");
		lblUserid.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblUserid.setBounds(25, 40, 78, 14);
		getContentPane().add(lblUserid);
		
		JLabel lblPassword = new JLabel("PASSWORD");
		lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblPassword.setBounds(25, 85, 86, 14);
		getContentPane().add(lblPassword);
		
		
		textField_1 = new JTextField();
		textField_1.setBounds(126, 84, 86, 20);
		getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource()=="cancel")
					flag=0;
					/* -= Eli =-Need to Add close window option */
				else
				{
					flag=1;
					try {
						int answer=0;
						user=textField.getText();
						pass=textField_1.getText();
						answer = Controler.HandleLoginRequestFromGui(user, pass);
						int k=2;
						if(100 == answer)
						{
							JOptionPane.showMessageDialog(null, "User ID does not exists in the system..");
						}
						else if(99 == answer)
						{
							JOptionPane.showMessageDialog(null,
								    "The password does not match the user name..",
								    "Login error",
								    JOptionPane.WARNING_MESSAGE);
						}
						else
							{
							JOptionPane.showMessageDialog(null, "Login Succesfull..!");
								switch(answer)
								{
								case 2:
								{
									StuMain = new Gui_Student_Entry_Window();
									StuMain.setSize(800, 800);
									StuMain.setBounds(100, 100, 900, 900);
									setContentPane(StuMain);
									StuMain.btnNewButton.addActionListener(new ActionListener() {
										public void actionPerformed(ActionEvent e) {
											StuMain.ExitProg();											
										}
									});
								}
								/* Eli: Here we will switch content pane to user main window according to the
								 * type returned from the database for that user..
								 */
								
								default:
									break;
								}
							}
						
						
						//log=new DBhandler();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					//log.login_method(textField.getText(),textField_1.getText());
					
				}
			}
		});
		btnOk.setBounds(89, 172, 89, 23);
		getContentPane().add(btnOk);
		
		JButton btnCancel = new JButton("CANCEL");
		btnCancel.setBounds(231, 172, 89, 23);
		getContentPane().add(btnCancel);
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);  // Eli added 3.1.2012
			}
		
		});
		
		
		

	}
	
	public void exit()
	{
		this.setVisible(false);
	}
}

